home *** CD-ROM | disk | FTP | other *** search
/ Nothing but Tetris / Nothing but Tetris.iso / amiga / workbench_tris / gameport.c < prev    next >
C/C++ Source or Header  |  1992-10-29  |  3KB  |  103 lines

  1. #include <exec/types.h>
  2. #include <exec/io.h>
  3. #include <exec/memory.h>
  4. #include <exec/exec.h>
  5. #include <devices/gameport.h>
  6. #include <devices/inputevent.h>
  7. /* */
  8. #include <clib/exec_protos.h>
  9.  
  10. /*-----------------------------------------------------------------------
  11. ** send a request to the gameport to read an event.
  12. */
  13. void 
  14. send_read_request(struct InputEvent * game_event, struct IOStdReq * game_io_msg)
  15. {
  16.     game_io_msg->io_Command = GPD_READEVENT;
  17.     game_io_msg->io_Flags = 0;
  18.     game_io_msg->io_Data = (APTR) game_event;
  19.     game_io_msg->io_Length = sizeof(struct InputEvent);
  20.     SendIO(game_io_msg);        /* Asynchronous - message will return later */
  21. }
  22.  
  23. /*-----------------------------------------------------------------------
  24. ** allocate the controller if it is available.
  25. ** you allocate the controller by setting its type to something
  26. ** other than GPCT_NOCONTROLLER.  Before you allocate the thing
  27. ** you need to check if anyone else is using it (it is free if
  28. ** it is set to GPCT_NOCONTROLLER).
  29. */
  30. BOOL 
  31. set_controller_type(BYTE type, struct IOStdReq *game_io_msg)
  32. {
  33.     BOOL            success = FALSE;
  34.     BYTE            controller_type = 0;
  35.  
  36.     /*
  37.      * begin critical section * we need to be sure that between the time
  38.      * we check that the controller * is available and the time we
  39.      * allocate it, no one else steals it.
  40.      */
  41.     Forbid();
  42.  
  43.     game_io_msg->io_Command = GPD_ASKCTYPE;        /* inquire current status */
  44.     game_io_msg->io_Flags = IOF_QUICK;
  45.     game_io_msg->io_Data = (APTR) & controller_type;    /* put answer in here */
  46.     game_io_msg->io_Length = 1;
  47.     DoIO(game_io_msg);
  48.  
  49.     /* No one is using this device unit, let's claim it */
  50.     if (controller_type == GPCT_NOCONTROLLER) {
  51.         game_io_msg->io_Command = GPD_SETCTYPE;
  52.         game_io_msg->io_Flags = IOF_QUICK;
  53.         game_io_msg->io_Data = (APTR) & type;
  54.         game_io_msg->io_Length = 1;
  55.         DoIO(game_io_msg);
  56.         success = TRUE;
  57.     }
  58.     Permit();                    /* critical section end */
  59.     return (success);
  60. }
  61.  
  62. /*-----------------------------------------------------------------------
  63. ** tell the gameport when to trigger.
  64. */
  65. void 
  66. set_trigger_conditions(struct GamePortTrigger * gpt, struct IOStdReq * game_io_msg)
  67. {
  68.     game_io_msg->io_Command = GPD_SETTRIGGER;
  69.     game_io_msg->io_Flags = IOF_QUICK;
  70.     game_io_msg->io_Data = (APTR) gpt;
  71.     game_io_msg->io_Length = (LONG) sizeof(struct GamePortTrigger);
  72.     DoIO(game_io_msg);
  73. }
  74.  
  75. /*-----------------------------------------------------------------------
  76. ** clear the buffer.  do this before you begin to be sure you
  77. ** start in a known state.
  78. */
  79. void 
  80. flush_buffer(struct IOStdReq *game_io_msg)
  81. {
  82.     game_io_msg->io_Command = CMD_CLEAR;
  83.     game_io_msg->io_Flags = IOF_QUICK;
  84.     game_io_msg->io_Data = NULL;
  85.     game_io_msg->io_Length = 0;
  86.     DoIO(game_io_msg);
  87. }
  88.  
  89. /*-----------------------------------------------------------------------
  90. ** free the unit by setting its type back to GPCT_NOCONTROLLER.
  91. */
  92. void 
  93. free_gp_unit(struct IOStdReq *game_io_msg)
  94. {
  95.     BYTE            type = GPCT_NOCONTROLLER;
  96.  
  97.     game_io_msg->io_Command = GPD_SETCTYPE;
  98.     game_io_msg->io_Flags = IOF_QUICK;
  99.     game_io_msg->io_Data = (APTR) & type;
  100.     game_io_msg->io_Length = 1;
  101.     DoIO(game_io_msg);
  102. }
  103.